/*******************************************************************************
 * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
 * @file     communicate_IIC.c
 * @author   UartTFT Application Team
 * @version  V0.0.1
 * @date     2023-01-11
 * @brief    This file provides UartTFT Application display function
 ********************************************************************************/

#include "communicate_IIC.h"
#include "iic.h"

#if (UARTBUS_OPTION == 3)

extern uint8_t flag_IIC_REG[120];

void LT_IIC_REG_Cmd(void)
{
	uint16_t i = 0;
	for (i = 0; i < sizeof(flag_IIC_REG);)
	{
		if (i == 0 && flag_IIC_REG[i] == 1)
		{
			Display_page(var[VAR_PAGE * 2] << 8 | var[VAR_PAGE * 2 + 1]);
			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 1 && flag_IIC_REG[i] == 1)
		{
			if (var[VAR_BL * 2 + 1] > 63)
				var[VAR_BL * 2 + 1] = 63;

			LT_BacklightSet(gDutyBuf[var[VAR_BL * 2 + 1]]);
			Backlight_flag = 1;

			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 2 && flag_IIC_REG[i] == 1)
		{
			if (var[(VAR_TIME + 0) * 2 + 1] > 99)
				var[(VAR_TIME + 0) * 2 + 1] = 99;
			if (var[(VAR_TIME + 1) * 2 + 1] > 12)
				var[(VAR_TIME + 1) * 2 + 1] = 12;
			if (var[(VAR_TIME + 2) * 2 + 1] > 31)
				var[(VAR_TIME + 2) * 2 + 1] = 31;
			if (var[(VAR_TIME + 3) * 2 + 1] > 23)
				var[(VAR_TIME + 3) * 2 + 1] = 23;
			if (var[(VAR_TIME + 4) * 2 + 1] > 59)
				var[(VAR_TIME + 4) * 2 + 1] = 59;
			if (var[(VAR_TIME + 5) * 2 + 1] > 59)
				var[(VAR_TIME + 5) * 2 + 1] = 59;

			RTC_Set(2000 + var[(VAR_TIME + 0) * 2 + 1], var[(VAR_TIME + 1) * 2 + 1], var[(VAR_TIME + 2) * 2 + 1], var[(VAR_TIME + 3) * 2 + 1], var[(VAR_TIME + 4) * 2 + 1], var[(VAR_TIME + 5) * 2 + 1]);
			flag_IIC_REG[i] = 0;
			i += 6;
		}
		else if (i == 8 && flag_IIC_REG[i] == 1)
		{
			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 9 && flag_IIC_REG[i] == 1)
		{
			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 10 && flag_IIC_REG[i] == 1)
		{
			var[VAR_WAV * 2] = ID_IIC_WAV >> 8;
			var[VAR_WAV * 2 + 1] = ID_IIC_WAV & 0xFF;

			Wav_DAC_Init((var[VAR_WAV * 2] << 8) | var[VAR_WAV * 2 + 1]);
			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 11 && flag_IIC_REG[i] == 1)
		{
			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 12 && flag_IIC_REG[i] == 1)
		{
			if (var[VAR_RTP * 2 + 1] == 0x5A)
			{
#if (LT_TOUCH_FLAG == 1)
				LT_TpAdjust();
#endif
				var[VAR_RTP * 2 + 1] = 0x00;
				Display_page(((var[VAR_PAGE * 2] << 8) | var[VAR_PAGE * 2 + 1]));
			}
			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 13 && flag_IIC_REG[i] == 1)
		{
			Button_Key_Code = (var[VAR_KEY * 2] << 8) | var[VAR_KEY * 2 + 1];
			Button_Key_Code_Flag = 1;
			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 14 && flag_IIC_REG[i] == 1)
		{
			if(var[2 * VAR_BLAUTO + 1] == 1)
			{
				if (Backlight_flag == 0)
				{
					Backlight_count = 0;
					Backlight_flag = 1;
					LT_BacklightSet(gDutyBuf[var[VAR_BL * 2 + 1]]);
				}
			}
			else if (var[2 * VAR_BLAUTO + 1] == 0)
			{
				Backlight_count = 0;
				Backlight_flag = 0;
				LT_BacklightSet(gDutyBuf[var[VAR_BL * 2 + 1]]);
			}
			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 15 && flag_IIC_REG[i] == 1)
		{
			if (Backlight_flag == 0 && var[2 * VAR_BLAUTO + 1] == 1)
			{
				LT_BacklightSet(gDutyBuf[var[VAR_BLSLEEP * 2 + 1]]);
			}
			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 16 && flag_IIC_REG[i] == 1)
		{
			Backlight_time = (var[2 * VAR_BLDURATION] << 8) + var[2 * VAR_BLDURATION + 1];
			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 17 && flag_IIC_REG[i] == 1)
		{
			if (var[2 * VAR_MCURESET] == 0xAA && var[2 * VAR_MCURESET + 1] == 0x55)
			{
				var[2 * VAR_MCURESET] = 0;
				var[2 * VAR_MCURESET + 1] = 0;

				APP(0x60000000);
			}
			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 0x3F && flag_IIC_REG[i] == 1)
		{
			if (var[2 * VAR_MTLANGUAGE + 1] < multi_language_count && var[2 * VAR_MTLANGUAGE + 1] != multi_language)
			{
				uint32_t language_sum = 0,language_count = 0,L_count = 0;
				uint32_t language_addr = 0,L_addr,L_len = 0;
				uint16_t L_num;
				uint8_t rebuf[2100];
				multi_language = var[2 * VAR_MTLANGUAGE + 1];

				if (addr_index[19] > 0)
				{
					LT_ReadFlash_UI(rebuf, addr_index[18] + 8 * multi_language, 8);
					language_addr = (rebuf[0]) + (rebuf[1] << 8) + (rebuf[2] << 16) + (rebuf[3] << 24);
					language_sum = (rebuf[4]) + (rebuf[5] << 8) + (rebuf[6] << 16) + (rebuf[7] << 24);
				}

				if (language_sum > 0)
				{
					if (language_sum - language_count >= 2048)
						LT_ReadFlash_UI(rebuf, language_addr, 2048);
					else
						LT_ReadFlash_UI(rebuf, language_addr, language_sum);

					while (1)
					{
						WDT_FeedDog();
						if (L_count + 4 > 2048)
						{
							if (language_sum - language_count >= 2048)
								LT_ReadFlash_UI(rebuf, language_addr + language_count, 2048);
							else
								LT_ReadFlash_UI(rebuf, language_addr + language_count, language_sum - language_count);
							L_count = 0;
						}
						L_addr = rebuf[L_count] + (rebuf[L_count + 1] << 8);
						L_len = rebuf[L_count + 2] + (rebuf[L_count + 3] << 8);
						L_count = L_count + 4;

						if (L_count + L_len > 2048)
						{
							if (language_sum - (language_count + 4) >= 2048)
								LT_ReadFlash_UI(rebuf, language_addr + language_count + 4, 2048);
							else
								LT_ReadFlash_UI(rebuf, language_addr + language_count + 4, language_sum - (language_count + 4));
							L_count = 0;
						}

						for (uint16_t ll = 0; ll < L_len; ll++)
						{
							var[L_addr * 2 + ll] = rebuf[L_count + ll];
						}
						language_count += L_len + 4;
						L_count += L_len;

						if (language_count >= language_sum)
							break;
					}
				}

				if(Pop_up_flag == 1)
				{
					Display_pop_up(Pop_up_id,Pop_up_x,Pop_up_y);
				}
				else Display_page(pic_id);
			}
			else
				var[2 * VAR_MTLANGUAGE + 1] = multi_language;

			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 0x40 && flag_IIC_REG[i] == 1)
		{
			if(lt_ui_change != var[VAR_UI_CHANGE * 2 + 1])
				LT_LCD_screen_dir(var[VAR_UI_CHANGE * 2 + 1]);

			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 0x42 && flag_IIC_REG[i] == 1)
		{
			if (var[2 * VAR_UI_UPDATE + 1] == 0 && UI_update == 1)
			{
				UI_update = 0;
				Address_Index(); 		// Get address index
				LT_ReadParam();
				if (Start_page > addr_index[7])
					Display_page(0);
				else
					Display_page(Start_page);
			}
			else if (var[2 * VAR_UI_UPDATE + 1] == 1 && UI_update == 0)
			{
				LT268_TFT_FillColor(0,0,LCD_XSIZE_TFT-1,LCD_YSIZE_TFT-1,Blue2);
				LT268_TFT_ShowAscll(0, 10, 24, 0, Red, White, (uint8_t *)"Update Flash");
				UI_update = 1;
			}
			flag_IIC_REG[i] = 0;
			i++;
		}
		else if (i == 0x44 && flag_IIC_REG[i] == 1)
		{
			lt_mcu_low_power();
			flag_IIC_REG[i] = 0;
			i++;
		}
		else
		{
			i++;
		}
	}
}

#endif
